const lanuchBrowser = require('./lanuchBrowser')
const getAppList = require('./getAppList')
const TaskDispatcher = require('./taskDispatcher')
const globalConfig = require('./config')
const getDriver = require('./getDriver')

module.exports = function startSpider (type) {
  let driver = getDriver()
  let url
  let info
  let config = globalConfig.config
  if (type === 'ios') {
    url = config.iosUrl
  } else {
    url = config.androidUrl
  }
  return getAppList(driver, url).then((list) => {
    info = list
    const dispatcher = new TaskDispatcher(info)
    console.info('列表抓取成功')
    return lanuchBrowser(config.browserNum, dispatcher, type)
    .then(() => {
      driver.quit()
      return info
    })
    .catch(() => {
      driver.quit()
    })
  })
}